#include "volumetool.h"
#include "ui_volumetool.h"

#include <QGraphicsDropShadowEffect>
#include <QPainter>
#include <QDebug>

VolumeTool::VolumeTool(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::VolumeTool)
{
    ui->setupUi(this);

    //设置窗口为：弹出窗口 窗口无边框 禁用窗口的阴影效果
    this->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground);

    //设置阴影效果
    QGraphicsDropShadowEffect* shadowEffect = new QGraphicsDropShadowEffect(this);
    shadowEffect->setColor("#646464");
    shadowEffect->setOffset(0,0);
    shadowEffect->setBlurRadius(10);

    this->setGraphicsEffect(shadowEffect);

    //设置图标
    ui->silenceBtn->setIcon(QIcon(":/images/volumn.png"));

    //设置文字 默认20%
    ui->volumeRatio->setText("20%");

    //默认20% outSlider(x , 180 - 180 * 0.2 + 25 ), 25为Slider和Box之间空隙
    ui->outSlider->setGeometry(ui->outSlider->x() , 180 - 36 + 25 , ui->outSlider->width() , 36);

    //设置按钮 在outSlider之上
    ui->sliderBtn->move(ui->sliderBtn->x() , ui->outSlider->y() - ui->sliderBtn->height() / 2);

    connect(ui->silenceBtn , &QPushButton::clicked , this , &VolumeTool::onSilenceBtnClicked);

    // 安装事件过滤器
    ui->volumeBox->installEventFilter(this);

}

VolumeTool::~VolumeTool()
{
    delete ui;
}

void VolumeTool::paintEvent(QPaintEvent *event)
{
    (void)event;

    //1. 创建绘图对象
    QPainter painter(this);

    // 2. 设置抗锯⻮
    painter.setRenderHint(QPainter::Antialiasing, true);

    //3. 设置画笔
    painter.setPen(Qt::NoPen);

    //4. 设置画刷
    painter.setBrush(Qt::white);

    //5. 计算位置
    QPolygon polygon;
    polygon.append(QPoint(10 , 309));
    polygon.append(QPoint(10 + 80 , 309));
    polygon.append(QPoint(10 + 40 , 318));

    //6. 绘制三角形
    painter.drawPolygon(polygon);
}

void VolumeTool::onSilenceBtnClicked()
{
    isMuted = !isMuted;
    if(isMuted == true)
    {
        ui->silenceBtn->setIcon(QIcon(":/images/silent.png"));
    }
    else
    {
        ui->silenceBtn->setIcon(QIcon(":/images/volumn.png"));
    }

    emit setSilence(isMuted);
}

bool VolumeTool::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == ui->volumeBox)
    {

        if(event->type() == QEvent::MouseButtonPress)
        {
            //鼠标按下
            setVolume();
        }
        else if(event->type() == QEvent::MouseMove)
        {
            //鼠标移动
            setVolume();
        }
        else if(event->type() == QEvent::MouseButtonRelease)
        {
            //鼠标释放
            emit setMusicVolume(volumeRatio);
        }
        return true;
    }
    return QObject::eventFilter(watched , event);
}

void VolumeTool::setVolume()
{
    //获取鼠标坐标
    int y = ui->volumeBox->mapFromGlobal(QCursor().pos()).y();
    if(y > 205)
        y = 205;
    if(y < 25)
        y = 25;

    //设置outline长度
    ui->outSlider->setGeometry(ui->outSlider->x() , y , ui->outSlider->width() , 205 - y);

    //设置sliderBtn位置
    ui->sliderBtn->move(ui->sliderBtn->x() , ui->outSlider->y() - ui->sliderBtn->height() / 2);

    //设置text百分比
    int height = ui->outSlider->height();

    volumeRatio = (int)(height / (double)180 * 100);
    ui->volumeRatio->setText(QString::number(volumeRatio) + "%");

}

